export const YMD = 'yyyy-MM-dd'

export const dateAdd = (type: string, number: number) => {
  var date = new Date()
  switch (type) {
    case 'y': {
      date.setFullYear(date.getFullYear() + number)
      break
    }
    case 'M': {
      date.setMonth(date.getMonth() + number)
      break
    }
    case 'd': {
      date.setDate(date.getDate() + number)
      break
    }
    case 'h': {
      date.setHours(date.getHours() + number)
      break
    }
    case 'm': {
      date.setMinutes(date.getMinutes() + number)
      break
    }
    case 's': {
      date.setSeconds(date.getSeconds() + number)
      break
    }
  }
  return dateToString(date, YMD)
}

export const dateToString = (date: Date, format: string) => {
  var dateStr = ''
  switch (format) {
    case YMD:
      dateStr = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)}`
      break
    case 'yyyy-MM-dd hh:mm:ss':
      dateStr = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`
      break
  }
  return dateStr
}
